/*! ******************************************************************************
 *
 * Created on 2022年3月29日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.yzbdl.lanius.ui.spoon.middle.ext.progress;

import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.pentaho.di.ui.core.PropsUI;

import com.google.common.collect.Maps;

/**
 * 进度视图组件
 * 
 * @author hs@yzbdl.ac.cn
 *
 */
public class ProgressComposite extends Composite {

    /**
     * 进度条名称集合
     */
    private Map<String, ProgressBarUnit> units = Maps.newHashMap();

    private final ScrolledComposite sc;

    private final Composite content;

    public ProgressComposite(Composite parent, int style) {
        super(parent, style);
        this.setLayout(new FillLayout());

        sc = new ScrolledComposite(this, SWT.H_SCROLL | SWT.V_SCROLL);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);

        content = new Composite(sc, SWT.NONE);
        content.setLayout(new GridLayout(1, true));

        sc.setContent(content);

        sc.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        content.layout();

        PropsUI.getInstance().setLook(sc);
        PropsUI.getInstance().setLook(content);

        ProgressCenter.addProgressComposites(this);
    }

    // public Composite getBarUnitParent() {
    // return content;
    // }

    public boolean containsBarUnit(String name) {
        return units.containsKey(name);
    }

    public ProgressBarUnit getBarUnit(String name) {
        return units.get(name);
    }

    public ProgressBarUnit addBarUnit(Progress progress) {
        ProgressBarUnit unit = new ProgressBarUnit(content, progress);
        units.put(progress.getName(), unit);
        return unit;
    }

    /**
     * 刷新界面
     */
    public void refreshUI() {
        sc.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        content.layout();
    }

    @Override
    public void dispose() {
        for (Entry<String, ProgressBarUnit> progress : units.entrySet()) {
            ProgressBarUnit unit = progress.getValue();
            if (unit != null && !unit.isDisposed()) {
                unit.dispose();
            }
        }
        if (sc != null && !sc.isDisposed()) {
            sc.dispose();
        }
        if (content != null && !content.isDisposed()) {
            content.dispose();
        }
        super.dispose();
    }

    /**
     * 获取units
     *
     * @return units
     */
    public Map<String, ProgressBarUnit> getUnits() {
        return units;
    }

    public void setUnits(Map<String, ProgressBarUnit> units) {
        this.units = units;
    }
}
